<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Payment_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
    
	function arrays($key){
		$payment_info = $this->get_one(array('nid'=>'sjzf'));
		$type = $payment_info['id'];

    	$data = array(
    		'var_payment_bank'  => array(
    									1=>array('channelName'=>'中国农业银行','channelCode'=>"ABC",'imageUrl'=>'zgnyyh.jpg','channelType'=>$type),
    									2=>array('channelName'=>'工商银行','channelCode'=>"ICBC",'imageUrl'=>'zggsyh.jpg','channelType'=>$type),
    									3=>array('channelName'=>'招商银行','channelCode'=>"CMB",'imageUrl'=>'zsyh.jpg','channelType'=>$type),
    									4=>array('channelName'=>'中国建设银行','channelCode'=>"CCB",'imageUrl'=>'zgjsyh.jpg','channelType'=>$type),
    									5=>array('channelName'=>'兴业银行','channelCode'=>"CIB",'imageUrl'=>'xyyh.jpg','channelType'=>$type),
    									6=>array('channelName'=>'广发银行','channelCode'=>"GDB",'imageUrl'=>'gfyh.jpg','channelType'=>$type),
    									7=>array('channelName'=>'中国民生银行','channelCode'=>"CMBC",'imageUrl'=>'zgmsyh.jpg','channelType'=>$type),
    									8=>array('channelName'=>'中信银行','channelCode'=>"ECITIC",'imageUrl'=>'zxyh.jpg','channelType'=>$type),
    									9=>array('channelName'=>'交通银行','channelCode'=>"BCOM",'imageUrl'=>'jtyh.jpg','channelType'=>$type),
    									10=>array('channelName'=>'中国邮政储蓄银行','channelCode'=>"PSBC",'imageUrl'=>'zgyzcxyh.jpg','channelType'=>$type),
    									11=>array('channelName'=>'中国银行','channelCode'=>"BOC",'imageUrl'=>'zgyh.jpg','channelType'=>$type),
    									12=>array('channelName'=>'中国光大银行','channelCode'=>"CEBB",'imageUrl'=>'zggdyh.jpg','channelType'=>$type),
    									13=>array('channelName'=>'深圳发展银行','channelCode'=>"SDB",'imageUrl'=>'sdb.png','channelType'=>$type),
    									14=>array('channelName'=>'平安银行','channelCode'=>"SPABANK",'imageUrl'=>'pab.png','channelType'=>$type),
    									15=>array('channelName'=>'华夏银行','channelCode'=>"HXB",'imageUrl'=>'hxyh.jpg','channelType'=>$type),
    									16=>array('channelName'=>'上海浦东収展银行','channelCode'=>"SPDB",'imageUrl'=>'pfyh.jpg','channelType'=>$type)),
    	);
    	
    	if($key && isset($data[$key])){
    		return $data[$key];
    	}else{
    		return array();
    	}
    }
    
    
	function add($data = array()){
		$this->db->insert('rsh_payment', $data);
		$this->mycache->delete('rsh_payment_list');
		return $this->db->insert_id();
	}
	
	function update($id, $data = array()){
		$this->db->where('id', $id);
		$this->db->update('rsh_payment', $data);
		$this->mycache->delete('rsh_payment_list');
		return $this->db->affected_rows();
	}
	
    function get_list($where = 0){
    	$key = 'rsh_payment_list';
    	$this->mycache->delete($key);
    	$list = $this->mycache->get_array($key);
    	if($list == false){
    		if($where > 0){
    			$this->db->where(array('fstatus'=>0));
    		}
    		
    		$this->db->order_by("payment_order", "asc"); 
    		$query = $this->db->get('rsh_payment');	
    		$result = $query->result_array();
    		if($result){
    			$list = array();
    			foreach($result as $val){
    				$list[$val['id']] = $val;
    			}
    			$this->mycache->set_array($key,$list);
    		}
    	}
		return $list;
    }
    
    
	
     
    function get_by_id($id){
		$key = 'rsh_payment_' . $id;
		$this->mycache->delete($key);//@todo
		$row = $this->mycache->get_array($key);
		if($row == false){
			$this->db->where('id',$id);
			$query = $this->db->get('rsh_payment');
			$row = $query->row_array();
			if($row){
				$this->mycache->set_array($key,$row);
			}
		}
		return $row;
	}
    
    function get_one($where = array(),$select='*'){
		$this->db->select($select);
		if($where){
			$this->db->where($where);
		}
		$this->db->limit(1);
		$query = $this->db->get('rsh_payment');
		return $query->row_array();
	}
	
	function get_msg_one($where = array(),$select='*'){
    	$this->db->select($select);
		if($where){
			$this->db->where($where);
		}
		$this->db->limit(1);
		$query = $this->db->get('rsh_msgonline');
		return $query->row_array();
	}
	
	function add_msg($data = array()){
		$this->db->insert('rsh_msgonline', $data);
		return $this->db->insert_id();
	}
	
	function update_msg($id, $data = array()){
		$this->db->where('id', $id);
		$this->db->update('rsh_msgonline', $data);
		return $this->db->affected_rows();
	}
	
	function submit($data){
		if(isset($data['payment']) && isset($data['money']) && isset($data['trade_no'])){
			$payment = $data['payment'];
			$parms['money'] = $data['money'];
			$parms['trade_no'] = $data['trade_no'];
			$parms['notify_url'] = 'http://pay.'.CON_WEBURL2.'/callback/getnotify';//通知地址
			$parms['return_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/callback/getreturn';//回调地址
			$parms['webname'] = $_SERVER['HTTP_HOST'];
			$parms['subject'] = isset($data['subject'])?$data['subject']:"";
			$parms['body'] = isset($data['body'])?$data['body']:"";
			$parms['trade_no'] = isset($data['trade_no'])?$data['trade_no']:"";
			
			
			$parms['bank'] = isset($data['bank'])?$data['bank']:"";
			$parms['bankAbbr'] =  isset($data['bankAbbr'])?$data['bankAbbr']:"";
			
			$paymentInfo = self::get_by_id($payment);
			if ($paymentInfo==false || $paymentInfo['config']=='') {
				return false;
			}else{
				$parms = unserialize($paymentInfo['config'])+$parms;
				$func = $paymentInfo['nid'];
				self::$func($parms);
			}
			
		}	
	}
	
	function ips($payment){
		//$submitUrl = 'https://pay.ips.net.cn/ipayment.aspx';//测试地址
		$submitUrl = 'https://pay.ips.com.cn/ipayment.aspx';//正式地址
		$Date = date("Ymd",time());
		$Currency_Type = "RMB";
		$Mer_key = $payment['PrivateKey'];
		$Amount = number_format($payment['money'], 2, '.', '');
		$Mer_code = $payment['member_id'];
		$Billno = $payment['trade_no'];
		$Gateway_Type = "01";
		$Lang = "GB";
		$Merchanturl = $payment['return_url'];
		$FailUrl = "";
		$ErrorUrl = "";
		$Attach = "";
		$DispAmount = $Amount;
		$OrderEncodeType = 5;
		$RetEncodeType = 17;
		$Rettype = 1;
		$ServerUrl = $payment['notify_url'];
		
		$SignMD5 = md5('billno'.$Billno.'currencytype'.$Currency_Type.'amount'.$Amount.'date'.$Date.'orderencodetype'.$OrderEncodeType.$Mer_key);
		
		echo "<html>";
		echo "<head>";
		echo "<title>Loading...</title>";
		echo "<meta http-equiv=\"content-Type\" content=\"text/html; charset=gb2312\" />";
		echo "</head>";
		echo "<body>";
		echo "<form action=\"{$submitUrl}\" method=\"post\" id=\"frm1\">";
		echo "<input type=\"hidden\" name=\"Mer_code\" value=\"{$Mer_code}\">";
		echo "<input type=\"hidden\" name=\"Billno\" value=\"{$Billno}\">";
		echo "<input type=\"hidden\" name=\"Amount\" value=\"{$Amount}\">";
		echo "<input type=\"hidden\" name=\"Date\" value=\"{$Date}\">";
		echo "<input type=\"hidden\" name=\"Currency_Type\" value=\"{$Currency_Type}\">";
		echo "<input type=\"hidden\" name=\"Gateway_Type\" value=\"{$Gateway_Type}\">";
		echo "<input type=\"hidden\" name=\"Lang\" value=\"{$Lang}\">";
		echo "<input type=\"hidden\" name=\"Merchanturl\" value=\"{$Merchanturl}\">";
		echo "<input type=\"hidden\" name=\"FailUrl\" value=\"{$FailUrl}\">";
		echo "<input type=\"hidden\" name=\"ErrorUrl\" value=\"{$ErrorUrl}\">";
		echo "<input type=\"hidden\" name=\"Attach\" value=\"{$Attach}\">";
		echo "<input type=\"hidden\" name=\"DispAmount\" value=\"{$DispAmount}\">";
		echo "<input type=\"hidden\" name=\"OrderEncodeType\" value=\"{$OrderEncodeType}\">";
		echo "<input type=\"hidden\" name=\"RetEncodeType\" value=\"{$RetEncodeType}\">";
		echo "<input type=\"hidden\" name=\"Rettype\" value=\"{$Rettype}\">";
		echo "<input type=\"hidden\" name=\"ServerUrl\" value=\"{$ServerUrl}\">";
		echo "<input type=\"hidden\" name=\"SignMD5\" value=\"{$SignMD5}\">";
		echo "</form>";
		echo "<script language=\"javascript\">";
		echo "document.getElementById(\"frm1\").submit();";      
		echo "</script>";
		echo "</body>";
		echo "</html>";
	}
	
	function baofoo($payment){
		//$submitUrl = 'https://gw.baofoo.com/payindex';//正式地址
		$submitUrl = 'https://vgw.baofoo.com/payindex';//测试地址
		$Date = date("Ymdhis");
		$Money = number_format($payment['money'], 2, '.', '')*100;
			
		$MemberID="100000178"; //$payment['member_id'];//商户号
		$TransID=$payment['trade_no'];//流水号
		$PayID=$payment['bankco'];//支付方式
		$TradeDate=$Date;//交易时间
		$OrderMoney=$Money;//订单金额
		$ProductName='一点投'; //产品名称
		$Amount=1; //商品数量
		$Username='';//支付用户名
		$AdditionalInfo='';//订单附加消息
		$PageUrl=$payment['return_url'];//通知商户页面端地址
		$ReturnUrl=$payment['notify_url'];//服务器底层通知地址
		$NoticeType=1;//通知类型	
		$Md5key=$payment['PrivateKey'];//md5密钥（KEY）
		$Md5key = "abcdefg";
		$MARK = "|";
		//MD5签名格式
		$Signature=md5($MemberID.$MARK.$PayID.$MARK.$TradeDate.$MARK.$TransID.$MARK.$OrderMoney.$MARK.$PageUrl.$MARK.$ReturnUrl.$MARK.$NoticeType.$MARK.$Md5key);
		$TerminalID = "20057"; 
		$TerminalID = "10000001";//测试
		$InterfaceVersion = "4.0";
		$KeyType = "1";

		$_SESSION['OrderMoney']=$OrderMoney; //设置提交金额的Session


		
		echo "<html>";
		echo "<head>";
		echo "<title>Loading...</title>";
		echo "<meta http-equiv=\"content-Type\" content=\"text/html; charset=UTF-8\" />";
		echo "</head>";
		echo "<body>";
		echo "<form action=\"{$submitUrl}\" method=\"post\" id=\"frm1\">";
		echo "<input type=\"hidden\" name=\"MemberID\" value=\"{$MemberID}\">";
		echo "<input type=\"hidden\" name=\"TerminalID\" value=\"{$TerminalID}\">";
		echo "<input type=\"hidden\" name=\"InterfaceVersion\" value=\"{$InterfaceVersion}\">";
		echo "<input type=\"hidden\" name=\"KeyType\" value=\"{$KeyType}\">";
		echo "<input type=\"hidden\" name=\"PayID\" value=\"{$PayID}\">";
		echo "<input type=\"hidden\" name=\"TradeDate\" value=\"{$TradeDate}\">";
		echo "<input type=\"hidden\" name=\"TransID\" value=\"{$TransID}\">";
		echo "<input type=\"hidden\" name=\"OrderMoney\" value=\"{$OrderMoney}\">";
		echo "<input type=\"hidden\" name=\"ProductName\" value=\"{$ProductName}\">";
		echo "<input type=\"hidden\" name=\"Amount\" value=\"{$Amount}\">";
		echo "<input type=\"hidden\" name=\"Username\" value=\"{$Username}\">";
		echo "<input type=\"hidden\" name=\"AdditionalInfo\" value=\"{$AdditionalInfo}\">";
		echo "<input type=\"hidden\" name=\"PageUrl\" value=\"{$PageUrl}\">";
		echo "<input type=\"hidden\" name=\"ReturnUrl\" value=\"{$ReturnUrl}\">";
		echo "<input type=\"hidden\" name=\"Signature\" value=\"{$Signature}\">";
		echo "<input type=\"hidden\" name=\"NoticeType\" value=\"{$NoticeType}\">";
		echo "</form>";
		echo "<script language=\"javascript\">";
		echo "document.getElementById(\"frm1\").submit();";      
		echo "</script>";
		echo "</body>";
		echo "</html>";
	}
    
	function huichao($payment){
		$submitUrl = 'https://pay.ecpss.com/sslpayment';//正式地址
		$MerNo     = $payment['member_id'];//商户号
		$MD5key    = $payment['PrivateKey'];//商户密匙
		$BillNo    = $payment['trade_no'];//流水号
		$Amount    = number_format($payment['money'], 2, '.', '');//金额
		$ReturnURL = 'http://www.zrcf.com.cn/callback/hcreturn';//商户通知地址  页面重定向地址
		$AdviceURL = 'http://www.zrcf.com.cn/callback/hcnotify';//底层通知地址
		$SignInfo  = md5($MerNo.'&'.$BillNo.'&'.$Amount.'&'.$ReturnURL.'&'.$MD5key);
		$SignInfo  = strtoupper($SignInfo);
		$orderTime = date('YmdHis');
		$defaultBankNumber = '';//银行代码
		$Remark    = '';//备注
		$products  = '一点投';//物品信息

		echo "<html>";
		echo "<head>";
		echo "<title>Loading...</title>";
		echo "<meta http-equiv=\"content-Type\" content=\"text/html; charset=UTF-8\" />";
		echo "</head>";
		echo "<body>";
		echo "<form action=\"{$submitUrl}\" method=\"post\" id=\"frm3\">";
		echo "<input type=\"hidden\" name=\"MerNo\" value=\"{$MerNo}\">";
		echo "<input type=\"hidden\" name=\"BillNo\" value=\"{$BillNo}\">";
		echo "<input type=\"hidden\" name=\"Amount\" value=\"{$Amount}\">";
		echo "<input type=\"hidden\" name=\"ReturnURL\" value=\"{$ReturnURL}\">";
		echo "<input type=\"hidden\" name=\"AdviceURL\" value=\"{$AdviceURL}\">";
		echo "<input type=\"hidden\" name=\"SignInfo\" value=\"{$SignInfo}\">";
		echo "<input type=\"hidden\" name=\"orderTime\" value=\"{$orderTime}\">";
		echo "<input type=\"hidden\" name=\"defaultBankNumber\" value=\"{$defaultBankNumber}\">";
		echo "<input type=\"hidden\" name=\"Remark\" value=\"{$Remark}\">";
		echo "<input type=\"hidden\" name=\"products\" value=\"{$products}\">";
		echo "</form>";
		echo "<script language=\"javascript\">";
		echo "document.getElementById(\"frm3\").submit();";      
		echo "</script>";
		echo "</body>";
		echo "</html>";
		
	}
    
	function chinabank($payment){
		$submitUrl = 'https://Pay3.chinabank.com.cn/PayGate';//正式地址
		
		$v_mid = $payment['member_id'];            //商户号
		$v_url = $payment['return_url'];           //返回地址
		$key   = $payment['PrivateKey']; //商户密钥
		
		$v_oid = $payment['trade_no'];             //订单号
		$v_amount = trim($payment['money']);       //支付金额     
		$v_moneytype = "CNY";                      //币种
		$text = $v_amount.$v_moneytype.$v_oid.$v_mid.$v_url.$key;        //md5加密拼凑串,注意顺序不能变
		$v_md5info = strtoupper(md5($text));                             //md5函数加密并转化成大写字母
		
		$remark1 = '';        //备注字段1
		$remark2 = '';        //备注字段2

		echo "<html>";
		echo "<head>";
		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
		echo "<title>ToChinaBank...</title>";
		echo "</head>";
		echo "<body>";
		echo "<form method=\"post\" id=\"frm2\" name=\"frm2\" action=\"{$submitUrl}\">";
		echo "<input type=\"hidden\" name=\"v_mid\"         value=\"{$v_mid}\">";
		echo "<input type=\"hidden\" name=\"v_oid\"         value=\"{$v_oid}\">";
		echo "<input type=\"hidden\" name=\"v_amount\"      value=\"{$v_amount}\">";
		echo "<input type=\"hidden\" name=\"v_moneytype\"   value=\"{$v_moneytype}\">";
		echo "<input type=\"hidden\" name=\"v_url\"         value=\"{$v_url}\">";
		echo "<input type=\"hidden\" name=\"v_md5info\"     value=\"{$v_md5info}\">";
		echo "<input type=\"hidden\" name=\"remark1\"       value=\"{$remark1}\">";
		echo "<input type=\"hidden\" name=\"remark2\"       value=\"{$remark2}\">";
		echo "</form>";
		echo "<script language=\"javascript\">";
		echo "document.getElementById(\"frm2\").submit();";      
		echo "</script>";
		echo "</body>";
		echo "</html>";
	}
	
	function sjzf($payment){
		if($payment['bank'] == 'payment_wg'){
			$this->payment_wg($payment);
		}else{
			$this->payment_sx($payment);
		}
	}
	
	//直接支付(银行网关)
	function payment_wg($payment){
		$type="GWDirectPay";  //直接支付(银行网关)
		$reqUrl = 'https://ipos.10086.cn/ips/cmpayService';
		$characterSet = '02';
		
	
		$ReturnURL = 'http://'.$_SERVER['HTTP_HOST'].'/callback/back_url';
		$AdviceURL = 'http://'.$_SERVER['HTTP_HOST'].'/callback/notifyurl';
		
		$callbackUrl  = $ReturnURL;
		$notifyUrl    = $AdviceURL;
		
		
		$ipAddress    = $this->input->ip_address();
		
		$merchantId   =  $payment['member_id'];
		$requestId    = strtotime("now");
		$signType     = 'MD5';
		$version      = '2.0.0';	
		
		$Money = number_format($payment['money'], 2, '.', '')*100;
			
		$amount 	    = $Money; //订单金额
		
		$bankAbbr     = $payment['bankAbbr']; //银行代码
		
		$currency     = '00'; //币种
		$orderDate    = date("Ymd",time()); //订单日期
		$orderId 	    = $payment['trade_no']; //商户订单号
		$merAcDate    = date("Ymd",time()); //户会计日期
		
		$period 	    = '30'; //有效期数量
		$periodUnit   = '00'; //有效期单位
		$merchantAbbr = ''; //商户展示名称
		$productDesc  = ''; //商品描述
		$productId    = ''; //商品编号
		$productName  = 'top up';//商品名称
		$productNum   = '';//商品数量
		$reserved1    = '';//保留字段1
		$reserved2    = '';//保留字段2
		$userToken    = '';//用户标识
		$showUrl 		  = '';//商品展示地址
		$couponsFlag  = '40'; 	//营销工具使用控制
		//设置商户请求报文参数 end
			
		//签名字段	
		$signData = $characterSet.$callbackUrl.$notifyUrl.$ipAddress.$merchantId.$requestId.$signType.$type
				      . $version     . $amount      . $bankAbbr    . $currency
				      . $orderDate   . $orderId     . $merAcDate   . $period 
				      . $periodUnit  . $merchantAbbr. $productDesc . $productId
				      . $productName . $productNum  . $reserved1   . $reserved2
				      . $userToken   . $showUrl     . $couponsFlag;			
	
		$signKey=$payment['PrivateKey']; //商户密钥;		
			
		//MD5方式签名
		$hmac=MD5sign($signKey,$signData);	
		
		
		
		echo "<html>";
		echo "<head>";
		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
		echo "<title>ToChinaBank...</title>";
		echo "</head>";
		echo "<body>";
		echo "<form method=\"post\" id=\"frm2\" name=\"frm2\" action=\"{$reqUrl}\">";
		echo "<input type=\"hidden\" name=\"characterSet\"         value=\"{$characterSet}\">";
		echo "<input type=\"hidden\" name=\"callbackUrl\"         value=\"{$callbackUrl}\">";
		echo "<input type=\"hidden\" name=\"notifyUrl\"      value=\"{$notifyUrl}\">";
		echo "<input type=\"hidden\" name=\"ipAddress\"   value=\"{$ipAddress}\">";
		echo "<input type=\"hidden\" name=\"merchantId\"         value=\"{$merchantId}\">";
		echo "<input type=\"hidden\" name=\"requestId\"     value=\"{$requestId}\">";
		echo "<input type=\"hidden\" name=\"signType\"       value=\"{$signType}\">";
		echo "<input type=\"hidden\" name=\"type\"       value=\"{$type}\">";
		echo "<input type=\"hidden\" name=\"version\"       value=\"{$version}\">";
		echo "<input type=\"hidden\" name=\"hmac\"       value=\"{$hmac}\">";
		echo "<input type=\"hidden\" name=\"amount\"       value=\"{$amount}\">";
		echo "<input type=\"hidden\" name=\"bankAbbr\"       value=\"{$bankAbbr}\">";
		echo "<input type=\"hidden\" name=\"currency\"       value=\"{$currency}\">";
		echo "<input type=\"hidden\" name=\"orderDate\"       value=\"{$orderDate}\">";
		echo "<input type=\"hidden\" name=\"orderId\"       value=\"{$orderId}\">";
		echo "<input type=\"hidden\" name=\"merAcDate\"       value=\"{$merAcDate}\">";
		echo "<input type=\"hidden\" name=\"period\"       value=\"{$period}\">";
		echo "<input type=\"hidden\" name=\"periodUnit\"       value=\"{$periodUnit}\">";
		echo "<input type=\"hidden\" name=\"merchantAbbr\"       value=\"{$merchantAbbr}\">";
		echo "<input type=\"hidden\" name=\"productDesc\"       value=\"{$productDesc}\">";
		echo "<input type=\"hidden\" name=\"productId\"       value=\"{$productId}\">";
		echo "<input type=\"hidden\" name=\"productName\"       value=\"{$productName}\">";
		echo "<input type=\"hidden\" name=\"productNum\"       value=\"{$productNum}\">";
		echo "<input type=\"hidden\" name=\"reserved1\"       value=\"{$reserved1}\">";
		echo "<input type=\"hidden\" name=\"reserved2\"       value=\"{$reserved2}\">";
		echo "<input type=\"hidden\" name=\"userToken\"       value=\"{$userToken}\">";
		echo "<input type=\"hidden\" name=\"showUrl\"       value=\"{$showUrl}\">";
		echo "<input type=\"hidden\" name=\"couponsFlag\"       value=\"{$couponsFlag}\">";
		echo "</form>";
		echo "<script language=\"javascript\">";
		echo "document.getElementById(\"frm2\").submit();";      
		echo "</script>";
		echo "</body>";
		echo "</html>";
	}
	
	
	
	//即时到账(双向确认)
	function payment_sx($payment){
		$type         = "DirectPayConfirm";	
		$reqUrl       =  'https://ipos.10086.cn/ips/cmpayService';
		$ipAddress    = $this->input->ip_address();
		$characterSet = '02';		
		
		$ReturnURL = 'http://'.$_SERVER['HTTP_HOST'].'/callback/back_url';
		$AdviceURL = 'http://'.$_SERVER['HTTP_HOST'].'/callback/notifyurl';
		
		$callbackUrl  = $ReturnURL;
		$notifyUrl    = $AdviceURL;
		
		$merchantId   =  $payment['member_id'];
		$requestId    = strtotime("now");
		$signType     = 'MD5';
		$version      = '2.0.0';	
		$Money = number_format($payment['money'], 2, '.', '')*100;
		$amount 	    = $Money; //订单金额
		$bankAbbr     = '';
		
		$currency     = '00'; //币种
		$orderDate    = date("Ymd",time()); //订单日期
		$orderId 	    = $payment['trade_no']; //商户订单号
		$merAcDate    = date("Ymd",time()); //户会计日期
		$period 	    = '30'; //有效期数量
		$periodUnit   = '00'; //有效期单位
		$merchantAbbr = ''; //商户展示名称
		$productDesc  = ''; //商品描述
		$productId    = ''; //商品编号
		$productName  = 'top up';//商品名称
		$productNum   = '';//商品数量
		$reserved1    = '';//保留字段1
		$reserved2    = '';//保留字段2
		$userToken    = '';//用户标识
		$showUrl 		  = '';//商品展示地址
		$couponsFlag  = ''; 	//营销工具使用控制
		//设置请求参数 end
	
		//组织签名数据	
		$signData = $characterSet.$callbackUrl.$notifyUrl.$ipAddress
		.$merchantId  .$requestId  .$signType .$type
		.$version     .$amount     .$bankAbbr .$currency
		.$orderDate   .$orderId    .$merAcDate .$period   .$periodUnit
		.$merchantAbbr.$productDesc.$productId.$productName
		.$productNum  .$reserved1  .$reserved2.$userToken
		.$showUrl     .$couponsFlag;			
	
		
		$signKey=$payment['PrivateKey']; //商户密钥;				
		//MD5方式签名				
		$hmac=MD5sign($signKey,$signData);
		
		$requestData = array();
		$requestData["characterSet"] = $characterSet;
		$requestData["callbackUrl"]  = $callbackUrl;
		$requestData["notifyUrl"]    = $notifyUrl;
		$requestData["ipAddress"]    = $ipAddress;
		$requestData["merchantId"]   = $merchantId;
		$requestData["requestId"]    = $requestId;
		$requestData["signType"]     = $signType;
		$requestData["type"]         = $type; 
		$requestData["version"]      = $version;
		$requestData["hmac"]         = $hmac;	 
		$requestData["amount"]       = $amount; 	      
		$requestData["bankAbbr"]     = $bankAbbr;      
		$requestData["currency"]     = $currency;      
		$requestData["orderDate"]    = $orderDate;     
		$requestData["orderId"]      = $orderId; 	 
		$requestData["merAcDate"]    = $merAcDate;   
		$requestData["period"]       = $period; 	      
		$requestData["periodUnit"]   = $periodUnit; 
		$requestData["merchantAbbr"] = $merchantAbbr;   
		$requestData["productDesc"]  = $productDesc;   
		$requestData["productId"]    = $productId;     
		$requestData["productName"]  = $productName;   
		$requestData["productNum"]   = $productNum;    
		$requestData["reserved1"]    = $reserved1;     
		$requestData["reserved2"]    = $reserved2;     
		$requestData["userToken"]    = $userToken;         	      
		$requestData["showUrl"] 	   = $showUrl; 		  
		$requestData["couponsFlag"]  = $couponsFlag;
	
		//http请求到手机支付平台
		
		$sTotalString = POSTDATA($reqUrl,$requestData);
		
		$recv = $sTotalString["MSG"];
		
		$recvArray = parseRecv($recv);
		
		$code=$recvArray["returnCode"];
		$payUrl;
		if ($code!="000000") {
			echo "code:".$code."</br>msg:".decodeUtf8($recvArray["message"]);
			exit();
		}
		else
		{
			$vfsign=$recvArray["merchantId"].$recvArray["requestId"]
			.$recvArray["signType"]  .$recvArray["type"]
			.$recvArray["version"]   .$recvArray["returnCode"]
			.$recvArray["message"]   .$recvArray["payUrl"];
			$hmac=MD5sign($signKey,$vfsign);
			$vhmac=$recvArray["hmac"];   
			if($hmac!=$vhmac)
			{
				echo "Signature verification failed!";
				exit();
			}
			else
			{
				$payUrl = $recvArray["payUrl"];
				//返回url处理
				$rpayUrl= parseUrl($payUrl);
			}     
		}
			  
		echo "<html>";
		echo "<head>";
		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
		echo "<title>DirectPayConfirm...</title>";
		echo "</head>";
		echo "<body>";
		echo "<form method=\"{$rpayUrl["method"]}\" id=\"frm2\" name=\"frm2\" action=\"{$rpayUrl["url"]}\">";
		echo "</form>";
		echo "<script language=\"javascript\">";
		echo "document.getElementById(\"frm2\").submit();";      
		echo "</script>";
		echo "</body>";
		echo "</html>";
	}
	
	function fengfu($payment){
		$submitUrl = 'https://www.sumapay.com/sumapay/unitivepay_bankPayForNoLoginUser';//正式地址
		$Date = date("Ymdhis");
		$Money    = number_format($payment['money'], 2, '.', '');//金额

		$requestId = $payment['trade_no'];
		$tradeProcess = $payment['member_id'];
		$totalBizType = 'BIZ01101';
		$totalPrice = $Money;
		$backurl = 'http://www.zrcf.com.cn/callback/backurl.html';
		$returnurl = 'http://www.zrcf.com.cn/account/recharge_log.html';
		$noticeurl = 'http://www.zrcf.com.cn/callback/noticeurl.html';
		$description = '';
		$rnaName = '';
		$rnaIdNumber = '';
		$rnaMobilePhone = '';
		$goodsDesc = '';
		$userIdIdentity = '';
		$allowRePay ='';
		$rePayTimeOut = '';
		$bankCardType = '';
		
		$productId = $payment['trade_no'];
		$productName = 'zczx';
		$fund = $Money;
		$merAcct =  $payment['member_id'];
		$bizType = 'BIZ01101';
		$productNumber = 1;
		
		$merKey = $payment['PrivateKey'];
		
		$sbOld = "";
		$sbOld = $sbOld . $requestId;
		$sbOld = $sbOld . $tradeProcess;
		$sbOld = $sbOld . $totalBizType;
		$sbOld = $sbOld . $totalPrice;
		$sbOld = $sbOld . $backurl;
		$sbOld = $sbOld . $returnurl;
		$sbOld = $sbOld . $noticeurl;
		$sbOld = $sbOld . $description;
		$signatrue = HmacMd5($sbOld, $merKey);


		
		echo "<html>";
		echo "<head>";
		echo "<title>Loading...</title>";
		echo "<meta http-equiv=\"content-Type\" content=\"text/html; charset=GBK\" />";
		echo "</head>";
		echo "<body>";
		echo "<form action=\"{$submitUrl}\" method=\"post\" id=\"frm1\">";
		echo "<input type=\"hidden\" name=\"requestId\" value=\"{$requestId}\">";
		echo "<input type=\"hidden\" name=\"tradeProcess\" value=\"{$tradeProcess}\">";
		echo "<input type=\"hidden\" name=\"mersignature\" value=\"{$signatrue}\">";
		echo "<input type=\"hidden\" name=\"totalBizType\" value=\"{$totalBizType}\">";
		echo "<input type=\"hidden\" name=\"totalPrice\" value=\"{$totalPrice}\">";
		echo "<input type=\"hidden\" name=\"backurl\" value=\"{$backurl}\">";
		echo "<input type=\"hidden\" name=\"returnurl\" value=\"{$returnurl}\">";
		echo "<input type=\"hidden\" name=\"noticeurl\" value=\"{$noticeurl}\">";
		echo "<input type=\"hidden\" name=\"description\" value=\"{$description}\">";
		echo "<input type=\"hidden\" name=\"rnaName\" value=\"{$rnaName}\">";
		echo "<input type=\"hidden\" name=\"rnaIdNumber\" value=\"{$rnaIdNumber}\">";
		echo "<input type=\"hidden\" name=\"rnaMobilePhone\" value=\"{$rnaMobilePhone}\">";
		echo "<input type=\"hidden\" name=\"goodsDesc\" value=\"{$goodsDesc}\">";
		echo "<input type=\"hidden\" name=\"userIdIdentity\" value=\"{$userIdIdentity}\">";
		echo "<input type=\"hidden\" name=\"allowRePay\" value=\"{$allowRePay}\">";
		echo "<input type=\"hidden\" name=\"rePayTimeOut\" value=\"{$rePayTimeOut}\">";
		echo "<input type=\"hidden\" name=\"bankCardType\" value=\"{$bankCardType}\">";
		echo "<input type=\"hidden\" name=\"productId\" value=\"{$productId}\">";
		echo "<input type=\"hidden\" name=\"productName\" value=\"{$productName}\">";
		echo "<input type=\"hidden\" name=\"fund\" value=\"{$fund}\">";
		echo "<input type=\"hidden\" name=\"bizType\" value=\"{$bizType}\">";
		echo "<input type=\"hidden\" name=\"merAcct\" value=\"{$merAcct}\">";
		echo "<input type=\"hidden\" name=\"productNumber\" value=\"{$productNumber}\">";
		echo "</form>";
		echo "<script language=\"javascript\">";
		echo "document.getElementById(\"frm1\").submit();";      
		echo "</script>";
		echo "</body>";
		echo "</html>";
	}
	
	public function epay($payment){
		$submitUrl = "https://www.95epay.cn/sslpayment";
		
		$MerNo 				= $payment['member_id'];
		$MD5key      	=    $payment['PrivateKey'];
		
		//$Money = number_format($payment['money'], 2, '.', '')*100;
		
		$Money = number_format($payment['money'], 2, '.', '');
		
		$Amount 			= $Money;
		$BillNo 			= $payment['trade_no'];
		
		$ReturnURL 		= 'http://www.zczx88.com/callback/payResult';
		$MD5info 			= getSignature($MerNo, $BillNo, $Amount, $ReturnURL, $MD5key);
	
		$NotifyURL 		= 'http://www.zczx88.com/callback/notifyResult';
		
		$PaymentType 	= "";
		
		$PayType 			= "CSPAY";//CSPAY:网银支付; 
				
		
		echo "<html>";
		echo "<head>";
		echo "<title>Loading...</title>";
		echo "<meta http-equiv=\"content-Type\" content=\"text/html; charset=gb2312\" />";
		echo "</head>";
		echo "<body>";
		echo "<form action=\"{$submitUrl}\" method=\"post\" id=\"frm1\">";
		echo "<input type=\"hidden\" name=\"MerNo\" value=\"{$MerNo}\">";
		echo "<input type=\"hidden\" name=\"Amount\" value=\"{$Amount}\">";
		echo "<input type=\"hidden\" name=\"BillNo\" value=\"{$BillNo}\">";
		echo "<input type=\"hidden\" name=\"ReturnURL\" value=\"{$ReturnURL}\">";
		echo "<input type=\"hidden\" name=\"NotifyURL\" value=\"{$NotifyURL}\">";
		echo "<input type=\"hidden\" name=\"MD5info\" value=\"{$MD5info}\">";
		echo "<input type=\"hidden\" name=\"PayType\" value=\"{$PayType}\">";
		echo "<input type=\"hidden\" name=\"PaymentType\" value=\"{$PaymentType}\">";
		echo "<input type=\"hidden\" name=\"MerRemark\" value=\"ddmoney pay\">";
		echo "<input type=\"hidden\" name=\"products\" value=\"ddmoney\">";
		echo "</form>";
		echo "<script language=\"javascript\">";
		echo "document.getElementById(\"frm1\").submit();";      
		echo "</script>";
		echo "</body>";
		echo "</html>";
	}
	
	function liandong($payment){
	
		$merDate = date ( "Ymd" );
		
		$Money    = number_format($payment['money'], 2, '.', '')*100;
		$service='pay_req_split_front';
		$charset='UTF-8';
		$mer_id=$payment['member_id'];
		$sign_type='RSA';
		$res_format="";
		$ret_url = 'http://www.zrcf.com.cn/callback/ldreturl.html';
		$notify_url = 'http://www.zrcf.com.cn/callback/ldnotifyurl.html';
		
		$version='4.0';
		$goods_id="";
		$goods_inf="";
		$media_id="";
		$media_type="";
		$order_id=$payment['trade_no'];
		$mer_date=$merDate;
		$amount=$Money;
		$amt_type='RMB';
		$pay_type="";
		$gate_id="";
		$mer_priv="";
		$user_ip="";
		$expire_time="";
		$expand="";
		require_once (APPPATH.'libraries/liandong/mer2Plat.php');
		$map =new HashMap();
		$map->put("service",$service);
		$map->put("charset",$charset);
		$map->put("mer_id",$mer_id);
		$map->put("sign_type",$sign_type);
		$map->put("ret_url",$ret_url);
		$map->put("notify_url",$notify_url);
		$map->put("goods_id",$goods_id);
		$map->put("goods_inf",$goods_inf);
		$map->put("media_id",$media_id);
		$map->put("media_type",$media_type);
		$map->put("order_id",$order_id);
		$map->put("mer_date",$mer_date);
		$map->put("res_format",$res_format);
		$map->put("amount",$amount);
		$map->put("amt_type",$amt_type);
		$map->put("pay_type",$pay_type);
		$map->put("gate_id",$gate_id);
		$map->put("mer_priv",$mer_priv);
		$map->put("user_ip",$user_ip);
		$map->put("expire_time",$expire_time);
		$map->put("expand",$expand);
		$map->put("version",$version);
		
		$reqData = MerToPlat::makeRequestDataByPost($map);//这个是重要的
		$sign = $reqData->getSign();//这个是为了在本DEMO中显示签名结果。
		$plain = $reqData->getPlain();//这个是为了在本DEMO中显示签名原串
		$submitUrl = $reqData->getUrl();
		
		
		echo "<head>";
		echo "<title>Loading...</title>";
		echo "<meta http-equiv=\"content-Type\" content=\"text/html; charset=utf-8\" />";
		echo "</head>";
		echo "<body>";
		echo "<form action=\"{$submitUrl}\" method=\"post\" id=\"frm1\">";
		echo "<input type=\"hidden\" name=\"amount\" value=\"{$amount}\">";
		echo "<input type=\"hidden\" name=\"amt_type\" value=\"{$amt_type}\">";
		echo "<input type=\"hidden\" name=\"charset\" value=\"{$charset}\">";
		echo "<input type=\"hidden\" name=\"mer_date\" value=\"{$mer_date}\">";
		echo "<input type=\"hidden\" name=\"mer_id\" value=\"{$mer_id}\">";
		echo "<input type=\"hidden\" name=\"notify_url\" value=\"{$notify_url}\">";
		echo "<input type=\"hidden\" name=\"order_id\" value=\"{$order_id}\">";
		echo "<input type=\"hidden\" name=\"ret_url\" value=\"{$ret_url}\">";
		echo "<input type=\"hidden\" name=\"service\" value=\"{$service}\">";		
		echo "<input type=\"hidden\" name=\"sign_type\" value=\"{$sign_type}\">";
		echo "<input type=\"hidden\" name=\"version\" value=\"{$version}\">";
		echo "<input type=\"hidden\" name=\"sign\" value=\"{$sign}\">";
		echo "<input type=\"hidden\" name=\"res_format\" value=\"{$res_format}\">";
		echo "<input type=\"hidden\" name=\"goods_id\" value=\"{$goods_id}\">";
		echo "<input type=\"hidden\" name=\"goods_inf\" value=\"{$goods_inf}\">";
		echo "<input type=\"hidden\" name=\"media_id\" value=\"{$media_id}\">";
		echo "<input type=\"hidden\" name=\"media_type\" value=\"{$media_type}\">";
		echo "<input type=\"hidden\" name=\"pay_type\" value=\"{$pay_type}\">";
		echo "<input type=\"hidden\" name=\"gate_id\" value=\"{$gate_id}\">";
		echo "<input type=\"hidden\" name=\"mer_priv\" value=\"{$mer_priv}\">";
		echo "<input type=\"hidden\" name=\"user_ip\" value=\"{$user_ip}\">";
		echo "<input type=\"hidden\" name=\"expand\" value=\"{$expand}\">";
		echo "<input type=\"hidden\" name=\"expire_time\" value=\"{$expire_time}\">";
		echo "</form>";
		echo "<script language=\"javascript\">";
		echo "document.getElementById(\"frm1\").submit();";      
		echo "</script>";
		echo "</body>";
		echo "</html>";
		exit;
	}
	
	
	//国付宝
	function gopay($payment){
		
		$submitUrl = 'https://mertest.gopay.com.cn/PGServer/Trans/WebClientAction.do';//正式地址
		$Date = date("Ymdhis");
		$Money = number_format($payment['money'], 2);
		$version = '2.1';          
		$charset = 2;          
		$language = 1;         
		$signType = 1;         
		$tranCode = '8888';   
		      
		$merchantID = $payment['member_id'];       		
		$merOrderNum = $payment['trade_no'];      
		$tranAmt = $Money;          
		$feeAmt = '';           
		$currencyType = '156';     
		$frontMerUrl = 'http://www.zczx88.com/callback/frontMerUrl';      
		$backgroundMerUrl = 'http://www.zczx88.com/callback/backgroundMerUrl'; 
		$orderDate    = date("YmdHis",time()); //订单日期
		$tranDateTime = $orderDate;     
		$virCardNoIn = 'jonsson7@163.com';
		//注意调试生产环境时需要修改这个值为生产参数
		  
		$tranIP = $this->js->get_ip();           
		$isRepeatSubmit = 1;   
		$goodsName = CON_NAME;        
		$goodsDetail = CON_WEBNAME;      
		$buyerName = '';        
		$buyerContact = '';     
		$merRemark1 = '';       
		$merRemark2 = '';      
		$bankCode = '';         
		$userType = '';   
		require_once (APPPATH.'libraries/guofubao/HttpClient.php');
		      
		$gopayServerTime = HttpClient::getGopayServerTime();
		$VerficationCode = $payment['PrivateKey'];
   	 	$signStr='version=['.$version.']tranCode=['.$tranCode.']merchantID=['.$merchantID.']merOrderNum=['.$merOrderNum.']tranAmt=['.$tranAmt.']feeAmt=['.$feeAmt.']tranDateTime=['.$tranDateTime.']frontMerUrl=['.$frontMerUrl.']backgroundMerUrl=['.$backgroundMerUrl.']orderId=[]gopayOutOrderId=[]tranIP=['.$tranIP.']respCode=[]gopayServerTime=['.$gopayServerTime.']VerficationCode=['.$VerficationCode.']';
		$signValue = md5($signStr);
		
		echo "<head>";
		echo "<title>Loading...</title>";
		echo "<meta http-equiv=\"content-Type\" content=\"text/html; charset=utf-8\" />";
		echo "</head>";
		echo "<body>";
		echo "<form action=\"{$submitUrl}\" method=\"post\" id=\"frm1\">";
		echo "<input type=\"hidden\" name=\"version\" value=\"{$version}\">";
		echo "<input type=\"hidden\" name=\"charset\" value=\"{$charset}\">";
		echo "<input type=\"hidden\" name=\"language\" value=\"{$language}\">";
		echo "<input type=\"hidden\" name=\"signType\" value=\"{$signType}\">";
		echo "<input type=\"hidden\" name=\"tranCode\" value=\"{$tranCode}\">";
		echo "<input type=\"hidden\" name=\"merchantID\" value=\"{$merchantID}\">";
		echo "<input type=\"hidden\" name=\"merOrderNum\" value=\"{$merOrderNum}\">";
		echo "<input type=\"hidden\" name=\"tranAmt\" value=\"{$tranAmt}\">";
		echo "<input type=\"hidden\" name=\"feeAmt\" value=\"{$feeAmt}\">";		
		echo "<input type=\"hidden\" name=\"currencyType\" value=\"{$currencyType}\">";
		echo "<input type=\"hidden\" name=\"frontMerUrl\" value=\"{$frontMerUrl}\">";
		echo "<input type=\"hidden\" name=\"backgroundMerUrl\" value=\"{$backgroundMerUrl}\">";
		echo "<input type=\"hidden\" name=\"tranDateTime\" value=\"{$tranDateTime}\">";
		echo "<input type=\"hidden\" name=\"virCardNoIn\" value=\"{$virCardNoIn}\">";
		echo "<input type=\"hidden\" name=\"tranIP\" value=\"{$tranIP}\">";
		echo "<input type=\"hidden\" name=\"isRepeatSubmit\" value=\"{$isRepeatSubmit}\">";
		echo "<input type=\"hidden\" name=\"goodsName\" value=\"{$goodsName}\">";
		echo "<input type=\"hidden\" name=\"goodsDetail\" value=\"{$goodsDetail}\">";
		echo "<input type=\"hidden\" name=\"buyerName\" value=\"{$buyerName}\">";
		echo "<input type=\"hidden\" name=\"buyerContact\" value=\"{$buyerContact}\">";
		echo "<input type=\"hidden\" name=\"merRemark1\" value=\"{$merRemark1}\">";
		echo "<input type=\"hidden\" name=\"merRemark2\" value=\"{$merRemark2}\">";
		echo "<input type=\"hidden\" name=\"signValue\" value=\"{$signValue}\">";
		echo "<input type=\"hidden\" name=\"bankCode\" value=\"{$bankCode}\">";
		echo "<input type=\"hidden\" name=\"userType\" value=\"{$userType}\">";
		echo "<input type=\"hidden\" name=\"gopayServerTime\" value=\"{$gopayServerTime}\">";
		echo "</form>";
		echo "<script language=\"javascript\">";
		echo "document.getElementById(\"frm1\").submit();";      
		echo "</script>";
		echo "</body>";
		echo "</html>";
		exit;
	}
	
}